trigger CreateAuditField on Audit__c (before insert,before update){
    List<Id> InStoreVisitId = new List<Id> ();
    for(audit__c t:trigger.new){
        InStoreVisitId.add(t.In_Store_Visit__c);
    
    }

        List<In_Store_Visit__c> InStoreVistList = [Select id,KAPromotion__c,NationalPromotion__c, otherPromotion__c,KA_Promotion_TSR__c,SSY_Promotion_TSR__c,Other_Promotion_TSR__c  From In_Store_Visit__c Where id in:InStoreVisitId];
        for(In_Store_Visit__c inS : InStoreVistList )
        {
            for(audit__c t:trigger.new){
                if(t.In_Store_Visit__c == inS.id){
                    t.KAPromotion_InStore__c = inS.KAPromotion__c;
                    t.NationalPromotion_InStore__c = inS.NationalPromotion__c;
                    t.OtherPromotion_InStore__c = inS.otherPromotion__c;
                    
                    //added By Yang Fangfang
                    t.KA_Promotion_TSR__c=inS.KA_Promotion_TSR__c;     
                    t.SSY_Promotion_TSR__c=inS.SSY_Promotion_TSR__c;
                    t.Other_Promotion_TSR__c=inS.Other_Promotion_TSR__c;

                      //SSY
                   List<String> SSYstorePromotionList=new List<String> ();      //In_Store_Visit__c中存储的variance   
                   String SSYpromotionStore=inS.NationalPromotion__c;
                   if(SSYpromotionStore!=null){
                      for(string str:SSYpromotionStore.split(';'))  {
                        SSYstorePromotionList.add(str);
                      }
                   }
                  List<String> SSYauditPromotionList=new List<String>(); //In_Store_Visit__c中存储的variance
                  String SSYpromotionAudit=t.NationalPromotion__c;
                  if(SSYpromotionAudit!=null){
                     for(string str:SSYpromotionAudit.split(';'))  {
                       SSYauditPromotionList.add(str);
                     }
                  }
                 
                  //判断具有相等的
                  Integer SSYnum=0;
                  for(String auditStr:SSYauditPromotionList){
                     for(String storeStr:SSYstorePromotionList){
                        if(storeStr==auditStr){
                          SSYnum+=1;
                          break;
                         }
                      }
                   }
                   t.SSYVariance__c=SSYnum;   
                   
                     //KA
                   List<String> KAstorePromotionList=new List<String> ();      //In_Store_Visit__c中存储的KAvariance   
                   String KApromotionStore=inS.KAPromotion__c;
                   if(KApromotionStore!=null){
                       for(string str:KApromotionStore.split(';'))  {
                       KAstorePromotionList.add(str);
                       }
                   }
 
                  List<String> KAauditPromotionList=new List<String>(); //AUDIT中存储的variance
                  String KApromotionAudit=t.KAPromotion__c;
                  if(KApromotionAudit!=null){
                      for(string str:KApromotionAudit.split(';'))  {
                       KAauditPromotionList.add(str);
                      }
                  }
                 
                  //判断具有相等的
                  Integer KAnum=0;
                  for(String auditStr:KAauditPromotionList){
                     for(String storeStr:KAstorePromotionList){
                        if(storeStr==auditStr){
                          KAnum+=1;
                          break;
                         }
                      }
                   }
                   t.KAVariance__c=KAnum;   
                   
                      //Other
                   List<String> OtherstorePromotionList=new List<String> ();      //In_Store_Visit__c中存储的KAvariance   
                   String OtherpromotionStore=inS.otherPromotion__c;
                   if(OtherpromotionStore!=null){
                      for(string str:OtherpromotionStore.split(';'))  {
                       OtherstorePromotionList.add(str);
                    }
                   }
                         
                  List<String> OtherauditPromotionList=new List<String>(); //AUDIT中存储的variance
                  String OtherpromotionAudit=t.otherPromotion__c;
                  if(OtherpromotionAudit!=null){
                     for(string str:OtherpromotionAudit.split(';'))  {
                       OtherauditPromotionList.add(str);
                   }
                  }
                 
                  //判断具有相等的
                  Integer Othernum=0;
                  for(String auditStr:OtherauditPromotionList){
                     for(String storeStr:OtherstorePromotionList){
                        if(storeStr==auditStr){
                          Othernum+=1;
                          break;
                         }
                      }
                   }
                   t.OtherVariance__c=Othernum;   
                }
            }
           
        }
}